<template>
  <div>
    <!-- 添加或修改资产清单表对话框 -->
    <el-dialog ref="editDialog" v-model="openFlag" :title="title" width="50%" append-to-body>
      <el-form ref="form" :model="formData" :rules="rules" label-width="108px" class="dialog-form-two">
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input v-model.trim="formData.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="UUID" prop="uuid">
              <el-input v-model.trim="formData.uuid" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="主机" prop="host">
              <el-input v-model.trim="formData.host" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="端口" prop="port">
              <el-input v-model.trim="formData.port" placeholder="请输入" />
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script setup>

import api from "./api.js";
import {computed, ref, getCurrentInstance} from "vue";

const { proxy } = getCurrentInstance();

const form = ref();
const openFlag = ref(false);
let isEdit = false;
const initFormData = {
  id: null,
  name: null,
  uuid: null,
  host: 'localhost',
  port: 8087,
};
const rules = {
  name: [
    { required: true, message: "不能为空", trigger: "blur" }
  ],
      uuid: [
    { required: true, message: "不能为空", trigger: "blur" }
  ],
      port: [
    { required: true, message: "不能为空", trigger: "blur" }
  ],
}
const title = computed(function () {
  return isEdit ? "编辑" : "新增"
})
const formData = ref({...initFormData});

// 重置
function reset() {
  formData.value = {...initFormData};
}

function open(currentRow) {
  openFlag.value = true;
  reset();

  if (currentRow) {
    isEdit = true;
    Object.assign(formData.value, currentRow);
    handleUpdate(currentRow);
  } else {
    isEdit = false;
  }
}

function close() {
  openFlag.value = false;
}

/** 提交按钮 */
function submitForm() {
  form.value.validate(valid => {
    if (valid) {
      if (isEdit) {
        api.update(formData.value).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openFlag = false;
          this.$emit("sub");
        });
      } else {
        api.add(formData.value).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.openFlag = false;
          this.$emit("sub");
        })
      }

    }
  });
}

defineExpose({
  open
})


</script>

